package com.yunfei.commonbase.hdutil.conreoller;

import com.alibaba.fastjson.JSONObject;
import com.yunfei.commonbase.hdutil.SFTPUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@RestController
@RequestMapping(value = "/sftp")
public class SftpController {


    @ApiOperation(value = "sftp文件上传",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "\t\"ip\": \"\",\n" +
            "\t\"port\": \"\",\n" +
            "\t\"username\": \"\",\n" +
            "\t\"password\": \"\",\n" +
            "\t\"sftpPath\": \"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip： sftpIP地址\n" +
            "port：sftp端口\n" +
            "username：sftp用户名\n" +
            "password：sftp密码\n" +
            "sftpPath：sftp路径\n" +
            "</pre>")
    @PostMapping(value = "/upload")
    public void sftpUpload(String requestJson, MultipartFile file) {
        try {
            JSONObject obj = JSONObject.parseObject(requestJson);
            String ip = obj.getString("ip");
            int port = obj.getInteger("port");
            String username = obj.getString("username");
            String password = obj.getString("password");
            String sftpPath = obj.getString("sftpPath");
            String filename = file.getOriginalFilename();
            InputStream is = file.getInputStream();
            SFTPUtils.upload(ip, port, username, password, 50000, is, sftpPath, filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "sftp文件下载",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "    \"ip\": \"\",\n" +
            "    \"port\": \"\",\n" +
            "    \"username\": \"\",\n" +
            "    \"password\": \"\",\n" +
            "    \"sftpPath\": \"\",\n" +
            "    \"filename\":\"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       sftpIP地址\n" +
            "port：     sftp端口\n" +
            "username： sftp用户名\n" +
            "password： sftp密码\n" +
            "sftpPath： sftp路径\n" +
            "filename:  文件名\n" +
            "</pre>")
    @PostMapping(value = "/download")
    public String sftpDownload(@RequestBody String requestJson, HttpServletResponse response) {
        try {
            JSONObject obj = JSONObject.parseObject(requestJson);
            String ip = obj.getString("ip");
            int port = obj.getInteger("port");
            String username = obj.getString("username");
            String password = obj.getString("password");
            String sftpPath = obj.getString("sftpPath");
            String filename = obj.getString("filename");
            response.setContentType("application/octet-stream");
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename, "UTF-8"));// 设置文件名
            OutputStream os = response.getOutputStream();
            SFTPUtils.download(ip, port, username, password, 50000, os, sftpPath + "/" + filename);
        } catch (Exception e) {
            return e.getMessage();
        }
        return "SUCCESS";
    }

    @ApiOperation(value = "sftp文件删除",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "\t\"ip\": \"\",\n" +
            "\t\"port\": \"\",\n" +
            "\t\"username\": \"\",\n" +
            "\t\"password\": \"\",\n" +
            "\t\"filename\":\"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       sftpIP地址\n" +
            "port：     sftp端口\n" +
            "username： sftp用户名\n" +
            "password： sftp密码\n" +
            "filename:  要删除的文件路径和名称\n" +
            "</pre>")
    @PostMapping(value = "/delete")
    public String delete(@RequestBody String requestJson) throws Exception {
        JSONObject obj = JSONObject.parseObject(requestJson);
        String ip = obj.getString("ip");
        int port = obj.getInteger("port");
        String username = obj.getString("username");
        String password = obj.getString("password");
//        String sftpPath = obj.getString("sftpPath");
        String filename = obj.getString("filename");
        SFTPUtils.deleteFile(ip,port,username,password,5000,filename);
        return "success";
    }


    @ApiOperation(value = "sftp获取文件列表",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "    \"ip\": \"\",\n" +
            "    \"port\": \"\",\n" +
            "    \"username\": \"\",\n" +
            "    \"password\": \"\",\n" +
            "    \"directory\": \"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       sftpIP地址\n" +
            "port：     sftp端口\n" +
            "username： sftp用户名\n" +
            "password： sftp密码\n" +
            "directory：查看的目录第一个/要带\n" +
            "</pre>")
    @PostMapping(value = "/listFiles")
    public List<String> listFiles(@RequestBody String requestJson) throws Exception {
        JSONObject obj = JSONObject.parseObject(requestJson);
        String ip = obj.getString("ip");
        int port = obj.getInteger("port");
        String username = obj.getString("username");
        String password = obj.getString("password");
//        String sftpPath = obj.getString("sftpPath");
        String directory = obj.getString("directory");
        List<String> list = SFTPUtils.listFiles(ip, port, username, password, 5000, directory);
        return list;
    }

    @ApiOperation(value = "删除文件夹下所有文件",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "    \"ip\": \"\",\n" +
            "    \"port\": \"\",\n" +
            "    \"username\": \"\",\n" +
            "    \"password\": \"\",\n" +
            "    \"directory\": \"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       sftpIP地址\n" +
            "port：     sftp端口\n" +
            "username： sftp用户名\n" +
            "password： sftp密码\n" +
            "directory：sftp删除的目录\n" +
            "</pre>")
    @PostMapping(value = "/deleteAllSFTP")
    public String deleteAllSFTPFile(@RequestBody String requestJson) throws Exception {
        JSONObject obj = JSONObject.parseObject(requestJson);
        String ip = obj.getString("ip");
        int port = obj.getInteger("port");
        String username = obj.getString("username");
        String password = obj.getString("password");
//        String sftpPath = obj.getString("sftpPath");
        String directory = obj.getString("directory");
        SFTPUtils.deleteAllSFTPFile(ip, port, username, password, 5000, directory);
        return "success";
    }


}
